package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.PermissionsChecker;
import com.diudiutechnology.www.yadiudiu.util.utils.PictureUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.SelectPhotoPop;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.UserInfoUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.Util_File;
import com.diudiutechnology.www.yadiudiu.util.view.CircleImageView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.MineFragmentModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * Created by Administrator on 2017/8/17.
 * 个人资料
 */

public class PersonalDataActivity extends Activity implements View.OnClickListener , Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.nickname_layout)//昵称
            RelativeLayout nicknameLayout;
    @Bind(R.id.nickname_text)//昵称
            TextView nickname_text;
    @Bind(R.id.cipher_layout)//修改密码
            RelativeLayout cipherLayout;
    @Bind(R.id.cipher_text)
    TextView cipher_text;

    @Bind(R.id.mobilephone_layout)//修改手机号
            RelativeLayout mobilephoneLayout;
    @Bind(R.id.mobilephone_text)//手机号
            TextView mobilephone_text;

    @Bind(R.id.pay_password_layout)//修改支付密码
            RelativeLayout pay_password_layout;
    @Bind(R.id.pay_password_text)//手机号
            TextView pay_password_text;


    @Bind(R.id.shipping_address_layout)//收货地址
            RelativeLayout shippingAddressLayout;
    @Bind(R.id.shipping_address)
            TextView shipping_address;

    @Bind(R.id.bank_card_layout)//银行卡
            RelativeLayout bankCardLayout;
    @Bind(R.id.bank_card_text)
    TextView bank_card_text;

    @Bind(R.id.education_layout)//学历
            RelativeLayout education_layout;
    @Bind(R.id.education_text)
    TextView education_text;

    @Bind(R.id.driver_license_layout)//驾驶证
            RelativeLayout driver_license_layout;
    @Bind(R.id.driver_license_text)
    TextView driver_license_text;

    @Bind(R.id.driving_permit_layout)//行驶证
            RelativeLayout driving_permit_layout;
    @Bind(R.id.driving_permit_text)
    TextView driving_permit_text;

    @Bind(R.id.in_the_address_layout)//现居地址
            RelativeLayout in_the_address_layout;
    @Bind(R.id.in_the_address_text)
    TextView in_the_address_text;
    @Bind(R.id.head_portrait_img)//头像
            CircleImageView head_portrait_img;

    Context context;
    private SelectPhotoPop selectPhotoPop;
    private View contentView;
    private MyProgressDialog mDialog;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    MineFragmentModel model;//缓存
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contentView = View.inflate(this, R.layout.activity_personal_data,
                null);
        setContentView(contentView);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        context = this;
        retur.setOnClickListener(this);
        nicknameLayout.setOnClickListener(this);
        cipherLayout.setOnClickListener(this);
        mobilephoneLayout.setOnClickListener(this);
        shippingAddressLayout.setOnClickListener(this);
        bankCardLayout.setOnClickListener(this);
        education_layout.setOnClickListener(this);
        driver_license_layout.setOnClickListener(this);
        driving_permit_layout.setOnClickListener(this);
        in_the_address_layout.setOnClickListener(this);
        head_portrait_img.setOnClickListener(this);
        pay_password_layout.setOnClickListener(this);
        options = ImgUtils.mImageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));

    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private  void initData(){
     model = UserInfoUtils.readUserInfo(this);
    imageLoader.displayImage(Constant.HTTP+model.getAvatar(), head_portrait_img, options);
    //姓名
    if (!Sundry.getStinges(model.getNickname())){
        nickname_text.setText(model.getNickname());
    }else {
        nickname_text.setText(getResources().getString(R.string.name));
    }
    //电话
    if (!Sundry.getStinges(model.getPhone())){
        String string = Sundry.getphon(model.getPhone()).toString();
        mobilephone_text.setText(string);
    }
    if (!Sundry.getStinges(model.getPassword())){
    cipher_text.setText("******");
    }
    //银行卡
    if (!model.getBankNumber().equals("0")){
        bank_card_text.setText("");
    }
    //现居地址
    if (!model.getCurrentsuite().equals("")){
        in_the_address_text.setText(model.getCurrentsuite());
    }else {
        in_the_address_text.setText("去填写");
    }
    //收货地址
    if (!model.getAddresscount().equals("0")){
        shipping_address.setText("");
    }else {
        shipping_address.setText("去填写");
    }
    //学历认证
    if (model.getIs_educationcerti().equals("2")){
        education_text.setText("");
    }else {
        education_text.setText("去认证");
    }
    //驾驶证
    if (!model.getDrivinglicense().equals("")){
        driver_license_text.setText("");
    }else {
        driver_license_text.setText("去认证");
    }
    //行驶证
    if (!model.getDrivingpermitface().equals("")){
        driving_permit_text.setText("");
    }else {
        driving_permit_text.setText("去认证");
    }

}
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.nickname_layout://昵称
                intent.setClass(context, NicknameChangeActivity.class);
                startActivity(intent);
                break;
            case R.id.mobilephone_layout://修改手机号
                intent.setClass(context, PasswordChangeActivity.class);
                intent.putExtra("pos", "0");
                startActivity(intent);
                break;
            case R.id.cipher_layout://修改密码
                intent.setClass(context, PasswordChangeActivity.class);
                intent.putExtra("pos", "1");
                startActivity(intent);
                break;
            case R.id.pay_password_layout://修改支付密码
                intent.setClass(context, PasswordChangeActivity.class);
                intent.putExtra("pos", "2");
                startActivity(intent);
                break;

            case R.id.shipping_address_layout://收货地址
                intent.setClass(context, ShippingAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.bank_card_layout://银行卡
                intent.setClass(context, SelectBankCardActivity.class);
                startActivity(intent);
                break;
            case R.id.education_layout://学历
                if (model.getIs_educationcerti().equals("2")){
                    intent.setClass(context, EducationActivity.class);
                    startActivity(intent);
                }else {
                    processIntent("0");
                }

                break;
            case R.id.driver_license_layout://驾驶证
                processIntent("1");
                break;
            case R.id.driving_permit_layout://行驶证
                processIntent("2");
                break;
            case R.id.in_the_address_layout://现居地址
                intent.setClass(context, InTheAddressActivity.class);
                startActivity(intent);
                break;
            case R.id.head_portrait_img://头像
                if (!PermissionsChecker.getPermissionsChecker(this, "0", 0)) {
                    if (!PermissionsChecker.getPermissionsChecker(this, "0", 2)) {
                        showSelectPicPop();
                    } else {
                        ToastUtils.show(context, "请检查是否打开储存权限");
                    }
                } else {
                    ToastUtils.show(context, "请检查是否打开相机权限");
                }
                break;
       		/* 注释  相机*/

            case R.id.item_popupwindows_camera:
                selectPhotoPop.dismiss();
                GalleryFinal.openCamera(Constant.CAMERA, MyApplication.getInstance().initGalley(Constant.CAMERA,1), mOnHanlderResultCallback);
                break;
            /* 注释  相册*/
            case R.id.item_popupwindows_Photo:
                selectPhotoPop.dismiss();
                GalleryFinal.openGallerySingle(Constant.GALLERYSINGLE, MyApplication.getInstance().initGalley(Constant.GALLERYSINGLE,1), mOnHanlderResultCallback);
                break;
            /* 注释  取消*/
            case R.id.item_popupwindows_cancel:
                selectPhotoPop.dismiss();
                break;
        }
    }

    // 跳页面 0、学历 1、驾驶证  2、行驶证
    private void processIntent(String pos) {
        Intent intent = new Intent(context, IdentityActivity.class);
        intent.putExtra("pos", pos);
        startActivity(intent);

    }
    //相册
    private void showSelectPicPop() {
        selectPhotoPop = new SelectPhotoPop(this, this);
        selectPhotoPop.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);
    }
        private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {

            Bitmap bitmap = PictureUtil.decodeSampledBitmapFromFile(resultList.get(0).getPhotoPath(),  Constant.WIDE, Constant.WIDE);

       String con   =PictureUtil.saveBitmapFile(bitmap, String.valueOf(0));

            System.out.println("stringmd5"+con);
            System.out.println("stringmd5压缩前的文件大小:"+ String.valueOf(Util_File.getFileOrFilesSize(resultList.get(0).getPhotoPath(), 2)) + "Kb");
            System.out.println("stringmd5压缩后的文件大小:"+String.valueOf(Util_File.getFileOrFilesSize(con, 2)) + "Kb");
            processpohoe(con);
        }
        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };
    //头像
    private void processpohoe(String pohoe){
        if (Sundry.isOpenNetwork(context)){
            mDialog =new MyProgressDialog(context);
            HttpClient.HeadPortrait(pohoe,this);

        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    @Override
    public void onSuccess(String result) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        System.out.println("stringmd5result"+result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote!=null||getnote.size()>0){
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")){
                String content = getnote.get(0).getContent();
                imageLoader.displayImage(Constant.HTTP+content, head_portrait_img, options);
                //UserInfoUtils.getInstans().updateavatar(context,content);
                ToastUtils.show(context, getMsg);
            }else if (status.equals("105")){
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            }else {
                ToastUtils.show(context, getMsg);
            }
        }
    }
    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog!=null){
            mDialog.dismiss();
        }
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}
